<template>
  <div>
    <!-- 头部 -->
    <CartHeader />
    <!-- 内容 -->
    <div class="shopCartContainer" v-if="goodsList.length">
      <div class="shopCartContent">
        <!-- 头部 -->
        <div class="shopCart-top">
          <div class="checkAll">
            <input
              class="chooseAll"
              type="checkbox"
              :checked="changeSelectedAll"
              @click="handleSelectedAll(!changeSelectedAll)"
            />
            全选
          </div>
          <div class="empty"></div>
          <div class="name">商品名称</div>
          <div class="price">单价</div>
          <div class="num">数量</div>
          <div class="count">小计</div>
          <div class="operation">操作</div>
        </div>
        <!-- 中间 -->
        <!-- 遍历渲染 -->
        <div
          class="shopCart-center"
          v-for="item in goodsList.length ? goodsList : []"
          :key="item.product_id"
        >
          <div class="checkAll">
            <input
              class="chooseAll"
              :checked="item.selected"
              type="checkbox"
              @click="changeSelected(!item.selected, item.product_id)"
            />
          </div>
          <div class="shopCart-img">
            <!-- https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1601215716.46047205.jpg?thumb=1&w=108&h=108 -->
            <img :src="item.images && item.images[0]" alt="" />
          </div>
          <div class="name">{{ item.name }}</div>
          <div class="price">{{ item.price_max }}</div>
          <!-- :value="item.count" -->
          <div class="num">
            <template>
              <el-input-number
                size="small"
                v-model="item.count"
                :min="1"
                :max="100"
                label="描述文字"
              ></el-input-number>
            </template>
          </div>
          <div class="count">{{ item.price_max * item.count }}元</div>
          <div class="operation" @click="delHandle(item.product_id)">x</div>
        </div>
        <!-- 底部 -->
        <div class="shopCart-down">
          <div class="down-left">
            <router-link to="/home">继续购物</router-link>
            <span class="down-line">|</span>
            <span>
              共
              <span class="down-num">{{ goodsList.length }}</span>
              件商品 , 已选择
              <span class="down-num">{{ selectedGoodsNum }}</span>
              件
            </span>
          </div>
          <div class="down-right">
            <div>
              合计:&nbsp;
              <span>{{ totalAllPrice }}</span>
              元
            </div>
            <router-link
              to="/pay"
              :class="totalAllPrice === 0 ? 'open' : 'close'"
            >
              去结算
              <span v-if="totalAllPrice === 0">
                请勾选需要结算的商品
                <span class="top"></span>
              </span>
            </router-link>
          </div>
        </div>
      </div>
    </div>
    <!-- 空购物车 -->
    <div class="empty-cart" v-else>
      <div class="conainer">
        <div class="iamge">
          <img
            src="https://cdn.cnbj1.fds.api.mi-img.com/staticsfile/cart/cart-empty.png"
            alt=""
          />
        </div>
        <div class="content-right">
          <div>您的购物车还是空的!</div>
          <router-link to="/search" class="label settlement">
            <span>马上去购物</span>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { reqGetGoodsDetail } from '@/api/detail'
import CartHeader from "@/components/CartHeader";
import { mapState, mapMutations, mapGetters } from "vuex";
export default {
  name: "ShopCart",
  data() {
    return {
      isShow: false,
    };
  },
  props: {
    value: {
      type: Number,
      default: 1,
    },
  },
  components: {
    CartHeader,
  },
  // async mounted() {
  //   const res = await reqGetGoodsDetail(this.$route.query.id)
  //   // console.log('shopCart', res)
  //   if (!res) return
  //   this.ADDSHOPITEMMUTATION(res)
  // },
  computed: {
    ...mapState({
      goodsList: (state) => state.cart.cartList,
    }),
    ...mapGetters(["changeSelectedAll"]),

    // 选中商品数量
    selectedGoodsNum() {
      return this.goodsList?.reduce((pre, cur) => {
        return (pre += cur.selected ? cur.count : 0);
      }, 0);
    },
    // 合计价钱
    totalAllPrice() {
      return this.goodsList?.reduce((pre, cur) => {
        return (pre += cur.selected ? cur.count * cur.price_max : 0);
      }, 0);
    },
  },
  methods: {
    ...mapMutations([
      "ADDSHOPITEMMUTATION",
      "CHANGEISSELECTED",
      "DELETEGOODS",
      "SELETEDALLGOODS",
    ]),

    // 切换选中状态
    changeSelected(selected, id) {
      this.CHANGEISSELECTED({ selected, id });
    },

    // 删除
    delHandle(id) {
      this.DELETEGOODS(id);
    },

    // 点击全选
    handleSelectedAll(selected) {
      this.SELETEDALLGOODS(selected);
    },
  },
};
</script>

<style lang="less" scoped>
// .el-dropdown-link {
//   cursor: pointer;
//   color: #757575;
// }
// .el-icon-arrow-down {
//   font-size: 12px;
// }
// 内容
.shopCartContainer {
  width: 100%;
  margin: 0 auto;
  background-color: #f5f5f5;
  padding: 30px;
  box-sizing: border-box;
  .shopCartContent {
    width: 1226px;
    margin: 0 auto;
    // 头部
    .shopCart-top {
      display: flex;
      align-items: center;
      padding: 20px 0;
      border-bottom: 1px solid #e4e0e0;
      background-color: #fff;
      div {
        font-size: 15px;
        &.checkAll {
          width: 110px;
          margin-left: 30px;
        }
        &.empty {
          width: 120px;
        }
        &.name {
          width: 380px;
        }
        &.price {
          width: 158px;
        }
        &.num {
          width: 150px;
          text-align: center;
        }
        &.count {
          width: 120px;
          text-align: right;
        }
        &.operation {
          width: 80px;
          margin-left: 90px;
        }
      }
    }
    // 中间
    .shopCart-center {
      display: flex;
      align-items: center;
      padding: 20px 0;
      border-bottom: 1px solid #e4e0e0;
      background-color: #fff;
      div {
        font-size: 15px;
        &.checkAll {
          width: 110px;
          margin-left: 30px;
        }
        &.shopCart-img {
          width: 120px;
        }
        &.name {
          width: 380px;
        }
        &.price {
          width: 158px;
        }
        &.num {
          width: 150px;
          text-align: center;
        }
        &.count {
          width: 120px;
          text-align: right;
        }
        &.operation {
          width: 80px;
          margin-left: 90px;
        }
        img {
          width: 60px;
          height: 75px;
          vertical-align: middle;
        }
        a {
          display: inline-block;
          width: 30px;
          height: 30px;
          line-height: 30px;
          border: 1px solid #e4e0e0;
        }
        input {
          height: 28px;
        }
      }
    }
    // 底部
    .shopCart-down {
      position: relative;
      background-color: #fff;
      margin-top: 20px;
      font-size: 14px;
      display: flex;
      // justify-content: space-between;
      padding: 10px 0;
      .down-left {
        width: 900px;
        display: flex;
        align-items: center;
        color: #757575;
        a {
          padding-left: 30px;
        }
        a:hover {
          color: #ff6700;
          text-decoration: none;
        }
        .down-line {
          display: inline-block;
          padding: 0px 18px;
          color: #f5f5f5;
        }
        .down-num {
          color: #ff6700;
        }
      }
      // 底部-右边
      .down-right {
        display: flex;
        // justify-content: space-between;
        align-items: center;
        div {
          color: #ff6700;
          span {
            font-size: 25px;
          }
        }
        // 去结算框
        .close {
          position: absolute;
          right: 0;
          display: inline-block;
          height: 50px;
          width: 200px;
          line-height: 50px;
          text-align: center;
          font-size: 18px;
          margin-left: 15px;
          // color: #b0b0b0;
          color: #fff;
          // background-color: #e0e0e0;
          background-color: #ff6700;
          text-decoration: none;
          &:hover {
            transition: all 0.4s;
            background-color: rgb(242, 88, 7);
          }

          // 请勾选需要结算的商品框
          span {
            position: absolute;
            right: 0;
            top: -60px;
            display: inline-block;
            width: 200px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            border: 1px solid #ff6700;
            color: #ff6700;
            font-size: 14px;
            background-color: #fff;
            // 下箭头
            .top {
              position: absolute;
              display: inline-block;
              top: 50px;
              right: 50%;
              transform: translateX(50%);
              background-color: transparent;
              width: 0px;
              height: 0px;
              border: 10px;
              border-style: solid;
              border-color: red transparent transparent;
            }
          }
        }
        // 请勾选需要结算的商品
        .open {
          position: absolute;
          right: 0;
          display: inline-block;
          height: 50px;
          width: 200px;
          line-height: 50px;
          text-align: center;
          font-size: 18px;
          margin-left: 15px;
          color: #b0b0b0;
          background-color: #e0e0e0;
          text-decoration: none;
          &:hover {
            color: #e0e0e0;
            // background-color: #e0e0e0;
          }
          span {
            position: absolute;
            right: 0;
            top: -60px;
            display: inline-block;
            width: 200px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            border: 1px solid #ff6700;
            color: #ff6700;
            font-size: 14px;
            background-color: #fff;
            // 下箭头
            .top {
              position: absolute;
              display: inline-block;
              top: 50px;
              right: 50%;
              transform: translateX(50%);
              background-color: transparent;
              width: 0px;
              height: 0px;
              border: 10px;
              border-style: solid;
              border-color: red transparent transparent;
            }
          }
        }
      }
    }
  }
}
// 空购物车
.empty-cart {
  width: 100%;
  margin: 0 auto;
  background-color: #f5f5f5;
  padding: 30px;
  box-sizing: border-box;
  .conainer {
    width: 1226px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;

    .content-right {
      padding-left: 50px;
      div {
        color: #b0b0b0;
        font-size: 36px;
        padding-bottom: 20px;
        font-weight: bold;
      }
      .label {
        display: inline-block;
        height: 50px;
        width: 170px;
        line-height: 50px;
        text-align: center;
        font-size: 14px;
        color: #b0b0b0;
        &.settlement {
          background-color: #ff6700;
          color: #fff;
        }
      }
      .settlement:hover,
      .settlement:hover span {
        text-decoration: none;
        color: #fff;
        transition: all 0.4s;
        background-color: rgb(242, 88, 7);
      }
    }
  }
}
</style>
